/*
 * Description: 语言状态中心
 * Company: 联宝（合肥）电子科技有限公司
 * Author: Dongxu.Ni
 * Date: 2020/3/10 15:20
 */

import { initLanguage } from '../../lang/lang-set'
import { setAuthLanguage } from '../../utils/browser'

const state = {
  language: initLanguage()
}

const getters = {
  getLanguage: state => state.language,
  getLanguageDesc: state => {
    switch (state.language) {
      case 'en':
        return 'English'
      case 'zh-cn':
        return '中文简体'
      case 'zh-tw':
        return '中文繁体'
      default:
        return ''
    }
  }
}

const mutations = {
  SET_LANGUAGE: (state, language) => {
    state.language = language
  }
}

const actions = {
  changeLanguage({ commit }, newLanguage) {
    // 1.改变语言状态state
    commit('SET_LANGUAGE', newLanguage)
    // 2.修改浏览器当前用户语言 cookies 或者 session
    setAuthLanguage(newLanguage)
    // 3.通知api状态修改
    commit('api/SET_API_WITH_LANGUAGE', newLanguage, { root: true })
    // 4.通知menu状态修改
    commit('menu/SET_MENU_WITH_LANGUAGE', newLanguage, { root: true })
    // 5.通知dict状态修改
    commit('dict/SET_DICT_WITH_LANGUAGE', newLanguage, { root: true })
    // 6. 。。。等等
  }
}

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}
